/*This do file generates two results files.  The first has rloser and uloser scores for each party, in addition to rolled and stuffed scores, the averagevote margin for wins and losses, and the ratio of average win margin to average loss margin.  The second file contains a list of votes on which each partywas rolled or stuffed.*/version 7.0clearset mem 110mset maxvar 30000local x "402" /* number of the last vote*/local w "1"    /* number of first vote */local y "9"  /* number of highest party*/local z "1"   /*  number of lowest party*/local q "402" /*number of votes*/local country "Canada" /*name of country*/local congress "19941995" /*years of congress*/local output "c:\output\loser\" /*location to which results files should be written*/local path "c:\temp\" /*location to which temporary files should be written*/local votesfile "C:\WINDOWS\Desktop\Carey\loserdata/`country'`congress'byvote.dta" /*location of byvote file*/local deputyfile "C:\WINDOWS\Desktop\Carey\loserdata/`country'`congress'bydep.dta" /*location of bydep file*//*The following code gets the threshold value for each vote and saves it in thresh.dta */use "`votesfile'", cleargen vote=_nkeep vote threshsave "`path'\thresh.dta", replace	/*First we will count all of the yes votes for the j parties on each vote. We	select the first party and then count all yeses for all votes. This returns a variable for each vote, constant across all observations. 	Then we drop all but the first observation and every variable up to the first yes vote count. This leaves us with a count of yes votes for each vote. 	We then transpose the data set, create a vote count variable (tells you the vote number) and rename the parties' yes 	vote variable aye`j'. this leaves us with two variables (aye`j' and the vote number) and an observation for each vote.	We then save this as aye`j', and open up the original data set again. Finally, we merge all aye`j' data sets 	and save the big file as ayevotes.*/ use "`deputyfile'"forv j=`z'/`y'			{forv i=`w'/`x'		{	count if v`i'==1 & party==`j'gen yesv`i'_`j' =r(N)			}keep yesv*keep in 1xpose, clear rename v1 ayep`j'gen vote=_nsave "`path'\ayep`j'.dta", replaceuse "`deputyfile'", clear					}	/*Here we're going to merge all of the ayep`j' files*/use "`path'\ayep`z'.dta"forv j=`z+1'/`y'		{merge using "`path'\ayep`j'.dta"drop _merge					}	/*Now, we save the merged data set of all party ayevotes*/save "`path'\ayevotes.dta", replace	/*Now, we do the same thing for nay votes.*/use "`deputyfile'", clearforv j=`z'/`y'		{forv i=`w'/`x'		{	count if v`i'==0 & party==`j'gen nov`i'_`j' =r(N)			}keep nov*keep in 1xpose, clear rename v1 nayp`j'gen vote=_nsave "`path'\nayp`j'.dta", replaceuse "`deputyfile'", clear				}use "`path'\nayp`z'.dta", clearforv j=`z+1'/`y'	{merge using "`path'\nayp`j'.dta"drop _merge			}save "`path'\nayvotes.dta", replace	/*Now we do the same for abstentions*/use "`deputyfile'", clearforv j=`z'/`y'		{forv i=`w'/`x'		{	count if v`i'==5 & party==`j'gen abstv`i'_`j' =r(N)			}keep abstv*keep in 1xpose, clear rename v1 abstp`j'gen vote=_nsave "`path'\abstp`j'.dta", replaceuse "`deputyfile'", clear				}use "`path'\abstp`z'.dta", clearforv j=`z+1'/`y'	{merge using "`path'\abstp`j'.dta"drop _merge			}save "`path'\abstvotes.dta", replace	/*Now for no responses*/use "`deputyfile'", clearforv j=`z'/`y'		{forv i=`w'/`x'		{	count if v`i'==6 & party==`j'|v`i'==2 & party==`j'|v`i'==3 & party==`j'|v`i'==4 & party==`j'|v`i'==7 & party==`j'|v`i'==8 & party==`j'|v`i'==9 & party==`j'|v`i'==10 & party==`j'gen noresp`i'_`j' =r(N)			}keep noresp*keep in 1xpose, clear rename v1 noresp`j'gen vote=_nsave "`path'\noresp`j'.dta", replaceuse "`deputyfile'", clear				}use c:/temp/noresp`z', clearforv j=`z+1'/`y'	{merge using "`path'\noresp`j'.dta"drop _merge			}save "`path'\norespvotes.dta", replace	/*Now for the missing values*/use "`deputyfile'", clearforv j=`z'/`y'		{forv i=`w'/`x'		{	count if v`i'==. & party==`j'gen missing`i'_`j' =r(N)			}keep missing*keep in 1xpose, clear rename v1 missingp`j'gen vote=_nsave "`path'\missingp`j'.dta", replaceuse "`deputyfile'", clear				}use "`path'\missingp`z'.dta", clearforv j=`z+1'/`y'	{merge using "`path'\missingp`j'.dta"drop _merge			}save "`path'\missingvotes.dta", replaceuse "`path'\ayevotes.dta", clear	/*Now, we merge the vote files and the threshold information. 	I renamed this file partyvotes and saved it. It  	contains a variable for the vote count, one for the threshold, 	and then the other variables for the votes of each party on each vote. 	Since the ayevote file is open, I merge the other files with it.*/merge using "`path'\nayvotes.dtadrop _mergemerge using "`path'\abstvotes.dta"drop _mergemerge using "`path'\norespvotes.dta"drop _mergemerge using "`path'\missingvotes.dta"drop _mergemerge using "`path'\thresh.dta" drop _merge/* This code generates variables which contain the total numbers of yeses, nos, noresps, abstains, and missings for each vote.  Depending onthe threshold, the total number of votes is calculated using one of the commands below*/egen yes=rsum(ayep*)egen no=rsum(nayp*)egen noresp=rsum(noresp*)egen abstain=rsum(abst*)egen missing=rsum(missing*)gen totalvote1=yes+no if thresh==1gen totalvote238910=yes+no+abstain if thresh==2|thresh==3|thresh==8|thresh==9|thresh==10gen totalvote41112=yes+no+noresp+abstain if thresh==4|thresh==11|thresh==12gen totalvote567=yes+no+noresp if thresh==5|thresh==6|thresh==7/*The following code generates "willp" for party "`j" which represents the will of the party on each vote.  1 means aye, 0 means no.  0 is default.The will of the party is calcuated differently depending on the threshold of the vote.*/forv j=`z'/`y'	{gen willp`j'=1 if ayep`j'>nayp`j' & thresh==1replace willp`j'=0 if ayep`j'<=nayp`j' & thresh==1replace willp`j'=1 if ayep`j'>(nayp`j'+abstp`j') & thresh==2replace willp`j'=0 if ayep`j'<=(nayp`j'+abstp`j') & thresh==2replace willp`j'=1 if ayep`j'>(nayp`j'+abstp`j') & thresh==3replace willp`j'=0 if ayep`j'<=(nayp`j'+abstp`j') & thresh==3replace willp`j'=1 if ayep`j'>(nayp`j'+abstp`j'+noresp`j') & thresh==4replace willp`j'=0 if ayep`j'<=(nayp`j'+abstp`j'+noresp`j') & thresh==4replace willp`j'=1 if ayep`j'>(nayp`j'+noresp`j') & thresh==5replace willp`j'=0 if ayep`j'<=(nayp`j'+noresp`j') & thresh==5replace willp`j'=1 if ayep`j'>(nayp`j'+noresp`j') & thresh==6replace willp`j'=0 if ayep`j'<=(nayp`j'+noresp`j') & thresh==6replace willp`j'=1 if ayep`j'>(nayp`j'+noresp`j') & thresh==7replace willp`j'=0 if ayep`j'<=(nayp`j'+noresp`j') & thresh==7replace willp`j'=1 if ayep`j'>(nayp`j'+abstp`j') & thresh==8replace willp`j'=0 if ayep`j'<=(nayp`j'+abstp`j') & thresh==8replace willp`j'=1 if ayep`j'>(nayp`j'+abstp`j') & thresh==9replace willp`j'=0 if ayep`j'<=(nayp`j'+abstp`j') & thresh==9replace willp`j'=1 if ayep`j'>(nayp`j'+abstp`j') & thresh==10replace willp`j'=0 if ayep`j'<=(nayp`j'+abstp`j') & thresh==10replace willp`j'=1 if ayep`j'>(nayp`j'+abstp`j'+noresp`j') & thresh==11replace willp`j'=0 if ayep`j'<=(nayp`j'+abstp`j'+noresp`j') & thresh==11replace willp`j'=1 if ayep`j'>(nayp`j'+abstp`j'+noresp`j') & thresh==12replace willp`j'=0 if ayep`j'<=(nayp`j'+abstp`j'+noresp`j') & thresh==12			}/* The following code generates a variable "results" which contains the % of yes votes, again calculating this variable differently for different thresholdvalues.  It then generates a variable "result" which indicates the result of the vote.  1 indicates pass, 2 indicates did not pass.*/gen results=yes/totalvote1 if thresh==1replace results= yes/totalvote238910 if thresh==2|thresh==3|thresh==8|thresh==9|thresh==10replace results= yes/totalvote41112 if thresh==4|thresh==11|thresh==12replace results= yes/totalvote567 if thresh==5|thresh==6|thresh==7replace results=0 if results==.gen result=1 if results >.5 & thresh==1replace result=0 if results <=.5 & thresh==1replace result=1 if results >.5 & thresh==2replace result=0 if results <=.5 & thresh==2replace result=1 if results >.67 & thresh==3replace result=0 if results <=.67 & thresh==3replace result=1 if results >.5 & thresh==4replace result=0 if results <=.5 & thresh==4replace result=1 if results >.57 & thresh==5replace result=0 if results <=.57 & thresh==5replace result=1 if results >.6 & thresh==6replace result=0 if results <=.6 & thresh==6replace result=1 if results >.67 & thresh==7replace result=0 if results <=.67 & thresh==7replace result=1 if results >.6 & thresh==8replace result=0 if results <=.6 & thresh==8replace result=1 if results >.75 & thresh==9replace result=0 if results <=.75 & thresh==9replace result=1 if results >.71 & thresh==10replace result=0 if results <=.71 & thresh==10replace result=1 if results >.67 & thresh==11replace result=0 if results <=.67 & thresh==11replace result=1 if results >.6 & thresh==12replace result=0 if results <=.6 & thresh==12/* The following code generates a variable "unityayep" for each party j that contains the % of votes that would have been a yes had the party votedyes with perfect unity.*/forv j=`z'/`y'		{gen unityayep`j'=((yes+nayp`j'+abstp`j'+noresp`j')/(totalvote1+abstp`j'+noresp`j')) if thresh==1replace unityayep`j'=((yes+nayp`j'+abstp`j'+noresp`j')/(totalvote238910+noresp`j')) if thresh==2|thresh==3|thresh==8|thresh==9|thresh==10replace unityayep`j'=((yes+nayp`j'+abstp`j'+noresp`j')/(totalvote41112)) if thresh==4|thresh==11|thresh==12replace unityayep`j'=((yes+nayp`j'+abstp`j'+noresp`j')/(totalvote567+abstp`j')) if thresh==5|thresh==6|thresh==7replace unityayep`j'=0 if unityayep`j'==.				}				/* The following code generates a variable "unitynayp" for each party j that contains the % of votes that would have been a no had the party votedno with perfect unity.*/forv j=`z'/`y'		{gen unitynayp`j'=(yes-ayep`j')/(totalvote1+abstp`j'+noresp`j') if thresh==1replace unitynayp`j'=(yes-ayep`j')/(totalvote238910+noresp`j') if thresh==2|thresh==3|thresh==8|thresh==9|thresh==10replace unitynayp`j'=(yes-ayep`j')/(totalvote41112) if thresh==4|thresh==11|thresh==12replace unitynayp`j'=(yes-ayep`j')/(totalvote567+abstp`j') if thresh==5|thresh==6|thresh==7replace unitynayp`j'=0 if unitynayp`j'==.				}/*The following code measures the margin by which each party won or lost.  A margin score is created for each vote, and then averaged over the totalnumber of votes*/forv j=`z'/`y'	{gen winby`j'=.replace winby`j'=abs(yes-no) if result==willp`j'gen loseby`j'=.replace loseby`j'=abs(yes-no) if result~=willp`j'egen ws`j'=sum(winby`j')egen ls`j'=sum(loseby`j')count if result==willp`j'gen wins`j'=r(N)count if result~=willp`j'gen losses`j'=r(N)gen wmargin`j'=ws`j'/wins`j'gen lmargin`j'=ls`j'/losses`j'gen marginratio`j'=wmargin`j'/lmargin`j'drop winby`j' loseby`j' ws`j' ls`j' wins`j' losses`j'}	/*Now we create the unity could have won scores.  These are 0 if perfect unity could not have altered the result of the vote in party j's favor, and 1 ifperfect unity would have altered the result of the vote in party j's favor */forv j=`z'/`y'		{gen unitychwp`j'=1 if thresh==1 & result!= willp`j' & willp`j'==1 & unityayep`j'>.5 replace unitychwp`j'=1 if thresh==1 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.5replace unitychwp`j'=0 if thresh==1 & unitychwp`j'==.replace unitychwp`j'=1 if thresh==2 & result!=willp`j' & willp`j'==1 & unityayep`j'>.5replace unitychwp`j'=1 if thresh==2 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.5replace unitychwp`j'=0 if thresh==2 & unitychwp`j'==. 	replace unitychwp`j'=1 if thresh==3 & result!=willp`j' & willp`j'==1 & unityayep`j'>.67replace unitychwp`j'=1 if thresh==3 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.67replace unitychwp`j'=0 if thresh==3 & unitychwp`j'==. replace unitychwp`j'=1 if thresh==4 & result!=willp`j' & willp`j'==1 & unityayep`j'>.5replace unitychwp`j'=1 if thresh==4 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.5replace unitychwp`j'=0 if thresh==4 & unitychwp`j'==. replace unitychwp`j'=1 if thresh==5 & result!=willp`j' & willp`j'==1 & unityayep`j'>.5replace unitychwp`j'=1 if thresh==5 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.5replace unitychwp`j'=0 if thresh==5 & unitychwp`j'==. replace unitychwp`j'=1 if thresh==6 & result!=willp`j' & willp`j'==1 & unityayep`j'>.6replace unitychwp`j'=1 if thresh==6 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.6replace unitychwp`j'=0 if thresh==6 & unitychwp`j'==. replace unitychwp`j'=1 if thresh==7 & result!=willp`j' & willp`j'==1 & unityayep`j'>.67replace unitychwp`j'=1 if thresh==7 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.67replace unitychwp`j'=0 if thresh==7 & unitychwp`j'==. replace unitychwp`j'=1 if thresh==8 & result!=willp`j' & willp`j'==1 & unityayep`j'>.6replace unitychwp`j'=1 if thresh==8 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.6replace unitychwp`j'=0 if thresh==8 & unitychwp`j'==. 	replace unitychwp`j'=1 if thresh==9 & result!=willp`j' & willp`j'==1 & unityayep`j'>.75replace unitychwp`j'=1 if thresh==9 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.75replace unitychwp`j'=0 if thresh==9 & unitychwp`j'==. 	replace unitychwp`j'=1 if thresh==10 & result!=willp`j' & willp`j'==1 & unityayep`j'>.71replace unitychwp`j'=1 if thresh==10 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.71replace unitychwp`j'=0 if thresh==10 & unitychwp`j'==. 	replace unitychwp`j'=1 if thresh==11 & result!=willp`j' & willp`j'==1 & unityayep`j'>.67replace unitychwp`j'=1 if thresh==11 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.67replace unitychwp`j'=0 if thresh==11 & unitychwp`j'==. replace unitychwp`j'=1 if thresh==12 & result!=willp`j' & willp`j'==1 & unityayep`j'>.6replace unitychwp`j'=1 if thresh==12 & result!=willp`j' & willp`j'==0 & unitynayp`j'<=.6replace unitychwp`j'=0 if thresh==12 & unitychwp`j'==. 			}			/*Now we create the riceloser scores.  This process is similar to the creation of the unityloser scores above*/			forv j=`z'/`y'		{gen riceayep`j'=((yes+nayp`j')/(totalvote1)) if thresh==1replace riceayep`j'=((yes+nayp`j')/(totalvote238910)) if thresh==2|thresh==3|thresh==8|thresh==9|thresh==10replace riceayep`j'=((yes+nayp`j')/(totalvote41112)) if thresh==4|thresh==11|thresh==12replace riceayep`j'=((yes+nayp`j')/(totalvote567)) if thresh==5|thresh==6|thresh==7replace riceayep`j'=0 if riceayep`j'==.				}				forv j=`z'/`y'		{gen ricenayp`j'=(yes-ayep`j')/(totalvote1) if thresh==1replace ricenayp`j'=(yes-ayep`j')/(totalvote238910) if thresh==2|thresh==3|thresh==8|thresh==9|thresh==10replace ricenayp`j'=(yes-ayep`j')/(totalvote41112) if thresh==4|thresh==11|thresh==12replace ricenayp`j'=(yes-ayep`j')/(totalvote567) if thresh==5|thresh==6|thresh==7replace ricenayp`j'=0 if ricenayp`j'==.				}forv j=`z'/`y'	{gen ricechwp`j'=1 if thresh==1 & result!=willp`j' & willp`j'==1 & riceayep`j'>.5replace ricechwp`j'=1 if thresh==1 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.5replace ricechwp`j'=0 if thresh==1 & ricechwp`j'==.replace ricechwp`j'=1 if thresh==2 & result!=willp`j' & willp`j'==1 & riceayep`j'>.5replace ricechwp`j'=1 if thresh==2 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.5replace ricechwp`j'=0 if thresh==2 & ricechwp`j'==. 	replace ricechwp`j'=1 if thresh==3 & result!=willp`j' & willp`j'==1 & riceayep`j'>.67replace ricechwp`j'=1 if thresh==3 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.67replace ricechwp`j'=0 if thresh==3 & ricechwp`j'==. replace ricechwp`j'=1 if thresh==4 & result!=willp`j' & willp`j'==1 & riceayep`j'>.5replace ricechwp`j'=1 if thresh==4 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.5replace ricechwp`j'=0 if thresh==4 & ricechwp`j'==. replace ricechwp`j'=1 if thresh==5 & result!=willp`j' & willp`j'==1 & riceayep`j'>.5replace ricechwp`j'=1 if thresh==5 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.5replace ricechwp`j'=0 if thresh==5 & ricechwp`j'==. replace ricechwp`j'=1 if thresh==6 & result!=willp`j' & willp`j'==1 & riceayep`j'>.6replace ricechwp`j'=1 if thresh==6 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.6replace ricechwp`j'=0 if thresh==6 & ricechwp`j'==. replace ricechwp`j'=1 if thresh==7 & result!=willp`j' & willp`j'==1 & riceayep`j'>.67replace ricechwp`j'=1 if thresh==7 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.67replace ricechwp`j'=0 if thresh==7 & ricechwp`j'==. replace ricechwp`j'=1 if thresh==8 & result!=willp`j' & willp`j'==1 & riceayep`j'>.6replace ricechwp`j'=1 if thresh==8 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.6replace ricechwp`j'=0 if thresh==8 & ricechwp`j'==. 	replace ricechwp`j'=1 if thresh==9 & result!=willp`j' & willp`j'==1 & riceayep`j'>.75replace ricechwp`j'=1 if thresh==9 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.75replace ricechwp`j'=0 if thresh==9 & ricechwp`j'==. 	replace ricechwp`j'=1 if thresh==10 & result!=willp`j' & willp`j'==1 & riceayep`j'>.71replace ricechwp`j'=1 if thresh==10 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.71replace ricechwp`j'=0 if thresh==10 & ricechwp`j'==. 	replace ricechwp`j'=1 if thresh==11 & result!=willp`j' & willp`j'==1 & riceayep`j'>.67replace ricechwp`j'=1 if thresh==11 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.67replace ricechwp`j'=0 if thresh==11 & ricechwp`j'==. replace ricechwp`j'=1 if thresh==12 & result!=willp`j' & willp`j'==1 & riceayep`j'>.6replace ricechwp`j'=1 if thresh==12 & result!=willp`j' & willp`j'==0 & ricenayp`j'<=.6replace ricechwp`j'=0 if thresh==12 & ricechwp`j'==. 							}/*The following code generates a variable "part" for each party j.  "Part" is 1 for a vote if a party had at least one yes, no, abstention, or no response, and0 if it did not*/ forv j=`z'/`y'		{gen part`j'=1 if (ayep`j'+nayp`j'+abstp`j'+noresp`j')>=1			}forv j=`z'/`y'		{replace part`j'=0 if part`j'==.				}/*  The following code tallies up the number of votes that each party took part in, and generates a variable "tkpart" for each party j.  Tkpart is 1 ifthe party participated in one or more votes, and 0 if it did not.*/forv j=`z'/`y'	{count if part`j'==1gen newpart`j'=r(N)			}forv j=`z'/`y'	{gen tkpart`j'=1 if newpart`j'>=1			}forv j=`z'/`y'	{replace tkpart`j'=0 if newpart`j'<1			}		/*The following code counts the number of votes that each party could have won had they voted with perfect unity, and then divides this number by thetotal number of votes*/forv j=`z'/`y'	{count if unitychwp`j'==1gen uchp`j'= r(N)/`q'			}	forv j=`z'/`y'	{count if ricechwp`j'==1gen rchwp`j'= r(N)/`q'			}	/*The following code generates a variable "percarryp" for each party j that contains the % of votes won.*/forv j=`z'/`y'	{count if willp`j'==result gen carryp`j'=r(N)		}forv j=`z'/`y'	{replace carryp`j'=0 if tkpart`j'==0			}forv j=`z'/`y'		{gen percarryp`j'=carryp`j'/`q'			}drop newpart* part*/*Now we save this file as penultimate.dta, drop some irrelevant data, and resave it as chwp.dta*/save "`path'\penultimate.dta", replacedrop a* will* result* vote mi* y* n* ri* uni* tot* thres* c*save "`path'\chwp.dta", replace /*The following code takes chwp.dta and drops everything besides the unity could have won scores.  It transposes the data, reads the party numberfrom the uchwp"j" variable, makes this party number an integer rather than a string, and sorts the data by party number.  It then saves this information inunitychwp.dta.  The same is done for each relevant variable.*/keep in 1drop r* perca* tkpart* lmargin* wmargin* margin*xpose, clear varnamegen str3 party=substr(_varname,5,.)drop _varnamedestring party, replacesort partyrename v1 unitychwpsave "`path'\unitychwp.dta", replaceuse "`path'\chwp.dta", clearkeep in 1drop u* perca* tkpart* lmargin* wmargin* margin*xpose, clear varnamegen str3 party=substr(_varname,6,.)drop _varnamedestring party, replacesort partyrename v1 ricechwpsave "`path'\ricechwp.dta", replaceuse "`path'\chwp.dta", clearkeep in 1drop u* r* tkpart* lmargin* wmargin* margin*xpose, clear varnamegen str3 party=substr(_varname,10,.)drop _varnamedestring party, replacesort partyrename v1 perwonsave "`path'\percarr.dta", replaceuse "`path'\chwp.dta", clearkeep in 1drop u* r* perca* lmargin* wmargin* margin*xpose, clear varnamegen str3 party=substr(_varname,7,.)drop _varnamedestring party, replacesort partyrename v1 partsave "`path'\tkpart.dta", replaceuse "`path'\chwp.dta", clearkeep in 1drop u* r* perca* tkpart* wmargin* margin*xpose, clear varnamegen str3 party=substr(_varname,8,.)drop _varnamedestring party, replacesort partyrename v1 lmarginsave "`path'\lmargin.dta", replaceuse "`path'\chwp.dta", clearkeep in 1drop u* r* perca* tkpart* lmargin* margin*xpose, clear varnamegen str3 party=substr(_varname,8,.)drop _varnamedestring party, replacesort partyrename v1 wmarginsave "`path'\wmargin.dta", replaceuse "`path'\chwp.dta", clearkeep in 1drop u* r* perca* tkpart* wmargin* lmargin*xpose, clear varnamegen str3 party=substr(_varname,12,.)drop _varnamedestring party, replacesort partyrename v1 marginratiosave "`path'\marginratio.dta", replace/* The following code uses penultimate.dta to generate rolled and stuffed scores by determining how many times a party was rolled or stuffed, and dividing by the total number of votes.  A party is stuffed when it wanted "yes," the result was "no," and it couldhave won had it voted "yes" with perfect unity.  A party is rolled when it wanted "no," the result was "yes," and it could have won had it voted "no" with perfect unity.  This code also saves a file in which a 1 indicates the votes on which party j was rolled or stuffed.*/use "`path'\penultimate.dta", clearforv j=`z'/`y'		{gen ustuffed`j'=1 if willp`j'==1 & unitychwp`j'==1replace ustuffed`j'=0 if ustuffed`j'==.count if ustuffed`j'==1gen totustuffed`j'=r(N)replace totustuffed`j'=0 if totustuffed`j'==.gen unitystuffed`j'=totustuffed`j'/`q'}save "`path'\ustuffedvotes.dta", replacekeep unitys*keep in 1xpose, clear varnamegen str3 party=substr(_varname,13,.)drop _varnamedestring party, replacesort partyrename v1 unitystuffedsave "`path'\ustuffed.dta", replaceuse "`path'\penultimate.dta", clearforv j=`z'/`y'		{gen rstuffed`j'=1 if willp`j'==1 & ricechwp`j'==1replace rstuffed`j'=0 if rstuffed`j'==.count if rstuffed`j'==1gen totrstuffed`j'=r(N)replace totrstuffed`j'=0 if totrstuffed`j'==.gen ricestuffed`j'=totrstuffed`j'/`q'}save "`path'\rstuffedvotes.dta", replacekeep rices*keep in 1xpose, clear varnamegen str3 party=substr(_varname,12,.)drop _varnamedestring party, replacesort partyrename v1 ricestuffedsave "`path'\rstuffed.dta", replaceuse "`path'\penultimate.dta", clearforv j=`z'/`y'		{gen urolled`j'=1 if willp`j'==0 & unitychwp`j'==1replace urolled`j'=0 if urolled`j'==.count if urolled`j'==1gen toturolled`j'=r(N)replace toturolled`j'=0 if toturolled`j'==.gen unityrolled`j'=toturolled`j'/`q'}save "`path'\urolledvotes.dta", replacekeep unityr*keep in 1xpose, clear varnamegen str3 party=substr(_varname,12,.)drop _varnamedestring party, replacesort partyrename v1 unityrolledsave "`path'\urolled.dta", replaceuse "`path'\penultimate.dta", clearforv j=`z'/`y'		{gen rrolled`j'=1 if willp`j'==0 & ricechwp`j'==1replace rrolled`j'=0 if rrolled`j'==.count if rrolled`j'==1gen totrrolled`j'=r(N)replace totrrolled`j'=0 if totrrolled`j'==.gen ricerolled`j'=totrrolled`j'/`q'}save "`path'\rrolledvotes.dta", replacekeep ricer*keep in 1xpose, clear varnamegen str3 party=substr(_varname,11,.)drop _varnamedestring party, replacesort partyrename v1 ricerolledsave "`path'\rrolled.dta", replaceuse "`path'\unitychwp.dta", clear/*The following code merges each of the previously created files into one results file called loser.dta.  It then drops parties that have no relevant data.*/merge using "`path'\ricechwp.dta"drop _mergemerge using "`path'\percarr.dta"drop _mergemerge using "`path'\rrolled.dta"drop _mergemerge using "`path'\rstuffed.dta"drop _mergemerge using "`path'\urolled.dta"drop _mergemerge using "`path'\ustuffed.dta"drop _mergemerge using "`path'\wmargin.dta"drop _mergemerge using "`path'\lmargin.dta"drop _mergemerge using "`path'\marginratio.dta"drop _mergedrop if  unitychwp==0 & ricechwp==0 &  (perwon==0 |  perwon==.) &  ricerolled==0 &  ricestuffed==0 &  unityrolled==0 &  unitystuffed==0rename unitychwp uloserrename ricechwp rloserorder party perwon uloser unityrolled unitystuffed rloser ricerolled ricestuffed wmargin lmargin marginratiosave "`output'\`country'`congress'loser.dta", replace/*The following code merges all of the data on rolled and stuffed votes (as opposed to rolled and stuffed scores) into one file rolledstuffed.dta */use "`path'\ustuffedvotes.dta", clearkeep ustuffed* votemerge using "`path'\rstuffedvotes.dta"drop _mergekeep ustuffed* rstuffed* votemerge using "`path'\urolledvotes.dta"drop _mergekeep ustuffed* rstuffed* urolled* votemerge using "`path'\rrolledvotes.dta"drop _mergekeep ustuffed* rstuffed* urolled* rrolled* votemerge using "`path'\penultimate.dta"keep ustuffed* rstuffed* urolled* rrolled* vote unitychwp* ricechwp*save "`path'\rolledstuffed.dta", replace/*The following code uses rolledstuffed.dta to isolate rolled and stuffed votes, and report the vote #s for which each party was rolled and stuffed.  On thefirst run through the loop (when j=z) it saves a new file loservotes, and on subsequent runs through the loop it merges new data into this file.*/forv j=`z'/`y'{use "`path'\rolledstuffed.dta", replacekeep if unitychwp`j' ==1 | ricechwp`j'==1 | ustuffed`j'==1 | urolled`j'==1 | rstuffed`j'==1 | rrolled`j'==1gen uloservote_`j'=vote if unitychwp`j'==1gen rloservote_`j'=vote if ricechwp`j'==1gen urolledvote_`j'=vote if urolled`j'==1gen ustuffedvote_`j'=vote if ustuffed`j'==1gen rrolledvote_`j'=vote if rrolled`j'==1gen rstuffedvote_`j'=vote if rstuffed`j'==1drop unitychwp`j' ricechwp`j' ustuffed`j' urolled`j' rstuffed`j' rrolled`j'keep uloservote* rloservote* ustuffedvote* urolledvote* rstuffedvote* rrolledvote*if(`j'==`z'){save "`output'\`country'`congress'loservotes.dta", replace}else {merge using "`output'\`country'`congress'loservotes.dta"drop _mergeaordersave "`output'\`country'`congress'loservotes.dta", replace}}